<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="微信小程序," />










<meta name="description" content="微信小程序标签： 微信小程序 微信小程序资料置顶: https://www.zhihu.com/question/50907897github项目地址: https://github.com/JepsonGithub/weapp_locally  [TOC] 从业务层面看小程序微信希望每天各种的需求, 都能通过微信解决 小程序希望有着服务号一样的功能, 跟应用程序一样的体验 它适用于用完即走的应用">
<meta name="keywords" content="微信小程序">
<meta property="og:type" content="article">
<meta property="og:title" content="微信小程序">
<meta property="og:url" content="http://yoursite.com/2018/01/28/微信小程序/index.html">
<meta property="og:site_name" content="Jepson_鹏鹏的专栏">
<meta property="og:description" content="微信小程序标签： 微信小程序 微信小程序资料置顶: https://www.zhihu.com/question/50907897github项目地址: https://github.com/JepsonGithub/weapp_locally  [TOC] 从业务层面看小程序微信希望每天各种的需求, 都能通过微信解决 小程序希望有着服务号一样的功能, 跟应用程序一样的体验 它适用于用完即走的应用">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://static.zybuluo.com/JepsonCC/gv1ixzeelsr6a370ro23xcgm/image_1c57nrh051s9p1h6j1jbkv3cdne9.png">
<meta property="og:image" content="http://static.zybuluo.com/JepsonCC/0itc0augj3km1zw9ciz2nlix/image_1c4rgt7lv1d1irv41ovoae6lec9.png">
<meta property="og:image" content="http://static.zybuluo.com/JepsonCC/x3oxomukswnrxe2x0aa8srsp/image_1c4rh4m5v1mma1din19g11j4n19nqm.png">
<meta property="og:image" content="http://static.zybuluo.com/JepsonCC/3x9ssoktx4wydhmid3tzbr1z/image_1c50e4cpg9hr19pvq041ttq1c7mm.png">
<meta property="og:updated_time" content="2018-02-06T07:12:48.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="微信小程序">
<meta name="twitter:description" content="微信小程序标签： 微信小程序 微信小程序资料置顶: https://www.zhihu.com/question/50907897github项目地址: https://github.com/JepsonGithub/weapp_locally  [TOC] 从业务层面看小程序微信希望每天各种的需求, 都能通过微信解决 小程序希望有着服务号一样的功能, 跟应用程序一样的体验 它适用于用完即走的应用">
<meta name="twitter:image" content="http://static.zybuluo.com/JepsonCC/gv1ixzeelsr6a370ro23xcgm/image_1c57nrh051s9p1h6j1jbkv3cdne9.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/01/28/微信小程序/"/>





  <title>微信小程序 | Jepson_鹏鹏的专栏</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Jepson_鹏鹏的专栏</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">Just do it!</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/01/28/微信小程序/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Jepson">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://static.zybuluo.com/JepsonCC/focrrncqb7uriops1uguvcfd/image_1c4uu558p1vhh71a1mvqbsp11i2m.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Jepson_鹏鹏的专栏">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">微信小程序</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-01-28T13:04:00+08:00">
                2018-01-28
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/微信小程序/" itemprop="url" rel="index">
                    <span itemprop="name">微信小程序</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="微信小程序"><a href="#微信小程序" class="headerlink" title="微信小程序"></a>微信小程序</h1><p>标签： 微信小程序</p>
<p>微信小程序资料置顶: <a href="https://www.zhihu.com/question/50907897" target="_blank" rel="noopener">https://www.zhihu.com/question/50907897</a><br>github项目地址: <a href="https://github.com/JepsonGithub/weapp_locally" target="_blank" rel="noopener">https://github.com/JepsonGithub/weapp_locally</a></p>
<hr>
<p>[TOC]</p>
<h2 id="从业务层面看小程序"><a href="#从业务层面看小程序" class="headerlink" title="从业务层面看小程序"></a>从业务层面看小程序</h2><p>微信希望每天各种的需求, 都能通过微信解决</p>
<p>小程序希望有着服务号一样的功能, 跟应用程序一样的体验</p>
<p>它适用于用完即走的应用, 比如说: 打车, 订餐, 买票, 话费充值等…</p>
<p>小程序不太适合中大型游戏, 使用频率特别高的应用(知乎), 一定要是用完即走, 即开即用的应用才是小程序的主场</p>
<h3 id="开发包必须小于-1-M"><a href="#开发包必须小于-1-M" class="headerlink" title="开发包必须小于 1 M"></a>开发包必须小于 1 M</h3><p>小程序有个限制就是应用开发包大小必须小于1M (这也是为什么不适合应用于大型应用的原因之一)<br>所以说小程序不用安装就能使用, 其实是不准确的, 因为其实还是下载了1M以内的应用包, 只是比较小, 用户感知不到而已</p>
<blockquote>
<p>总之一句话, 小程序希望实现让应用触手可及, 即用即开, 用完即走</p>
</blockquote>
<hr>
<h2 id="从技术层面看小程序"><a href="#从技术层面看小程序" class="headerlink" title="从技术层面看小程序"></a>从技术层面看小程序</h2><p>小程序不需要下载安装, 可以通过微信打开, 那他是怎么实现的呢?</p>
<p>小程序是要运行在微信内部的, 是微信提供的一个平台, 所有的规则都是微信制定的, 包括所有的标签用法, 标签展示效果, 基本js语法等等, 借助微信这样的运行环境, 就能开发出能在微信中跑的应用!</p>
<p>那么所有的用户只要安装了微信, 所有的应用都可以触手可得</p>
<p>开发者不用过多的考虑多终端的兼容性处理问题 (小程序对 ES6 以及 flex 的支持非常的好, 所以写起代码来会非常爽 )</p>
<hr>
<h2 id="微信小程序初体验"><a href="#微信小程序初体验" class="headerlink" title="微信小程序初体验"></a>微信小程序初体验</h2><p>我们会发现小程序自带的样式就是很好看的不需要我们去定制.<br>本身web端基础样式会丑是由于历史原因, 从一开始出来的时候, 这些控件就很丑.<br>小程序就不一样了, 他是一个新的平台, 所有样式什么的不受web端基础样式的限制, 很多控件默认样式就比较好看了, 这个是比较爽的</p>
<blockquote>
<p>小程序与web开发有思维上的共通, 从技术层面有很多不一样的地方, 还是需要注意的 (比如: 小程序中没有dom, 没有window, 没有ajax等, 它有自己的一套渲染语法, 有自己的request请求语法)</p>
</blockquote>
<p>开发工具下载地址: <a href="https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/download.html?t=2018125" target="_blank" rel="noopener">https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/download.html?t=2018125</a></p>
<ul>
<li>安装开发工具, 用绑定的微信账号扫一扫</li>
</ul>
<h3 id="基本配置"><a href="#基本配置" class="headerlink" title="基本配置"></a>基本配置</h3><p>小程序中约定大于配置: 小程序中的开发方式以及文件结构目录, 都是约定好了的, 每个目录什么作用等等, 不需要过多的配置, 核心就四个文件类型, </p>
<ul>
<li>js文件</li>
<li>json(配置文件)</li>
<li>wxml(页面标签等, 类似于html)</li>
<li>wxss(页面样式控制,类似于css)</li>
</ul>
<p><img src="http://static.zybuluo.com/JepsonCC/gv1ixzeelsr6a370ro23xcgm/image_1c57nrh051s9p1h6j1jbkv3cdne9.png" alt="image_1c57nrh051s9p1h6j1jbkv3cdne9.png-44.8kB"></p>
<p>比较简单, 目录结构说明什么的文档都有<br>文档地址: <a href="https://mp.weixin.qq.com/debug/wxadoc/dev/framework/config.html" target="_blank" rel="noopener">https://mp.weixin.qq.com/debug/wxadoc/dev/framework/config.html</a></p>
<h4 id="全局配置-app-json"><a href="#全局配置-app-json" class="headerlink" title="全局配置 (app.json)"></a>全局配置 (app.json)</h4><ul>
<li>配置顶部导航条文字:  <code>&quot;navigationBarTitleText&quot;: &quot;哈哈&quot;</code> </li>
<li>配置 bar颜色: <code>&quot;navigationBarTextStyle&quot;:&quot;white&quot;</code> 只支持 白色 和 黑色</li>
<li>配置下拉刷新 <code>&quot;enablePullDownRefresh&quot;</code>  boolean</li>
<li>配置debug 为 true, 会多很多 log 输出</li>
</ul>
<p><strong>tips: 注意页面的配置会优先于全局的配置, 也就是页面中json里面配的优先级会更高, 会层叠掉全局的配置</strong></p>
<h4 id="页面配置-页面-json"><a href="#页面配置-页面-json" class="headerlink" title="页面配置 (页面.json)"></a>页面配置 (页面.json)</h4><ul>
<li>页面中的配置有个小细节: 只能配置窗口相关的配置</li>
<li>页面中最常用的标签: view 相当于div, text 相当于 span</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span><span class="tag">&lt;<span class="name">text</span>&gt;</span>hello 大家好, 我是微信小程序<span class="tag">&lt;/<span class="name">text</span>&gt;</span><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span><span class="tag">&lt;<span class="name">text</span>&gt;</span>hello 大家好, 我是微信小程序<span class="tag">&lt;/<span class="name">text</span>&gt;</span><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h4 id="标签栏配置-app-json"><a href="#标签栏配置-app-json" class="headerlink" title="标签栏配置 (app.json)"></a>标签栏配置 (app.json)</h4><p>tabBar, 也就是底部选项卡, 非常简单, 这里来一个我配好的完整的给大家参考, 注意是不能加注释的, 所以下面就没有注释</p>
<p>在pages中写页面名称 <code>&quot;pages/list/list&quot;</code>, 然后保存, 开发工具会自动帮我们创建核心js,json,wxml,wxss等文件, 非常的方便</p>
<p>下面的配置其实看名字就能明白, 如果有不理解的可以移步文档查一下<br><a href="https://mp.weixin.qq.com/debug/wxadoc/dev/framework/config.html" target="_blank" rel="noopener">https://mp.weixin.qq.com/debug/wxadoc/dev/framework/config.html</a></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="string">"pages"</span>:[</span><br><span class="line">    <span class="string">"pages/index/index"</span>,</span><br><span class="line">    <span class="string">"pages/message/message"</span>,</span><br><span class="line">    <span class="string">"pages/profile/profile"</span>,</span><br><span class="line">    <span class="string">"pages/list/list"</span>    </span><br><span class="line">  ],</span><br><span class="line">  <span class="string">"window"</span>:&#123;</span><br><span class="line">    <span class="string">"backgroundTextStyle"</span>:<span class="string">"black"</span>,</span><br><span class="line">    <span class="string">"navigationBarBackgroundColor"</span>: <span class="string">"#3a4861"</span>,</span><br><span class="line">    <span class="string">"navigationBarTitleText"</span>: <span class="string">"本地生活"</span>,</span><br><span class="line">    <span class="string">"navigationBarTextStyle"</span>:<span class="string">"white"</span>,</span><br><span class="line">    <span class="string">"background"</span>: <span class="string">"#bcc0c9"</span>,</span><br><span class="line">    <span class="string">"enablePullDownRefresh"</span>: <span class="literal">false</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">"tabBar"</span>: &#123;</span><br><span class="line">    <span class="string">"color"</span>: <span class="string">"#999"</span>,</span><br><span class="line">    <span class="string">"selectedColor"</span>: <span class="string">"#444"</span>,</span><br><span class="line">    <span class="string">"backgroundColor"</span>: <span class="string">"#fff"</span>,</span><br><span class="line">    <span class="string">"borderStyle"</span>: <span class="string">"black"</span>,</span><br><span class="line">    <span class="string">"list"</span>: [</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="string">"pagePath"</span>: <span class="string">"pages/index/index"</span>,</span><br><span class="line">        <span class="string">"text"</span>: <span class="string">"首页"</span>,</span><br><span class="line">        <span class="string">"iconPath"</span>: <span class="string">"assets/tabs/home.png"</span>,</span><br><span class="line">        <span class="string">"selectedIconPath"</span>: <span class="string">"assets/tabs/home-active.png"</span></span><br><span class="line">      &#125;,</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="string">"pagePath"</span>: <span class="string">"pages/message/message"</span>,</span><br><span class="line">        <span class="string">"text"</span>: <span class="string">"消息"</span>,</span><br><span class="line">        <span class="string">"iconPath"</span>: <span class="string">"assets/tabs/message.png"</span>,</span><br><span class="line">        <span class="string">"selectedIconPath"</span>: <span class="string">"assets/tabs/message-active.png"</span></span><br><span class="line">      &#125;,</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="string">"pagePath"</span>: <span class="string">"pages/profile/profile"</span>,</span><br><span class="line">        <span class="string">"text"</span>: <span class="string">"我的"</span>,</span><br><span class="line">        <span class="string">"iconPath"</span>: <span class="string">"assets/tabs/profile.png"</span>,</span><br><span class="line">        <span class="string">"selectedIconPath"</span>: <span class="string">"assets/tabs/profile-active.png"</span></span><br><span class="line">      &#125;</span><br><span class="line">    ]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>需要注意的是: tabBar 至少存在两项, 这很好记, 只有一个 tabBar 也没什么意义</p>
<p>如果当前页面没有配置在 tabBar 中, 那么他下面就不会显示 tabBar</p>
<hr>
<h2 id="逻辑层js的小-tips"><a href="#逻辑层js的小-tips" class="headerlink" title="逻辑层js的小 tips"></a>逻辑层js的小 tips</h2><p>注意: 在小程序中, 没有 window<br>因为所谓的 bom 和 dom 都是相对有浏览器这个平台而言的, 所以在小程序这个平台中是没有 window的</p>
<h2 id="应用的生命周期"><a href="#应用的生命周期" class="headerlink" title="应用的生命周期"></a>应用的生命周期</h2><p>小程序采用 common.js 规范, 每一个js文件都有单独的作用域, 整个应用有自己的生命周期, 在不同的周期会调用不同的回调函数</p>
<p>App 中注册的全局对象中可以添加任意的成员, 这个app对象在全局是共享的, 在其他页面js中, 通过getApp()可以获取全局的 app 应用程序实例对象, 就可以拿到里面的存的属性和方法了<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 采用的是 common.js 的规范</span></span><br><span class="line"><span class="comment">// 小程序中每一个js文件都有单独的作用域</span></span><br><span class="line">App(&#123;</span><br><span class="line">  <span class="comment">// 整个应用启动时触发, 只触发一次</span></span><br><span class="line">  onLaunch: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">"应用启动了"</span>)</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 应用程序显示到屏幕上</span></span><br><span class="line">  onShow: <span class="function"><span class="keyword">function</span>(<span class="params"> options </span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log( options );</span><br><span class="line">    <span class="built_in">console</span>.log( <span class="string">"显示到屏幕上"</span> )</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 应用程序隐藏到后台触发</span></span><br><span class="line">  onHide: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log( <span class="string">"屏幕隐藏进入后台了"</span> );</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 一旦应用程序出错了, 就会触发</span></span><br><span class="line">  <span class="comment">// 注意: onError 只能捕获到运行中的错误</span></span><br><span class="line">  onError: <span class="function"><span class="keyword">function</span>(<span class="params"> message </span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log( message )</span><br><span class="line">  &#125;,</span><br><span class="line">  </span><br><span class="line">  <span class="comment">// 可以添加任意的从成员, app对象在全局是共享的, </span></span><br><span class="line">  <span class="comment">// 通过 getApp() 可以用来获取全局的app应用程序实例对象</span></span><br><span class="line">  foo: <span class="string">"bar"</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></p>
<ul>
<li>可以通过 onShow 判断 options 中的 scene编号, 来判断当前是通过哪种方式进行小程序的, 用以做不同的处理</li>
<li>app对象是全局共享的, 添加的成员, 将来通过 getApp() 方法可以直接获取到 app 应用程序实例对象</li>
</ul>
<h2 id="页面的生命周期"><a href="#页面的生命周期" class="headerlink" title="页面的生命周期"></a>页面的生命周期</h2><p>每个页面的有自己的生命周期, 包括页面的加载, 渲染, 卸载等等…<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 创建一个页面实例对象</span></span><br><span class="line">Page(&#123;</span><br><span class="line">  data: &#123;&#125;,</span><br><span class="line">  <span class="comment">// 页面加载: 这个页面即将要工作了, 适合做页面的数据初始化</span></span><br><span class="line">  onLoad() &#123;</span><br><span class="line">    <span class="comment">// this.data.foo = 'hello world'</span></span><br><span class="line">    <span class="keyword">this</span>.setData( &#123; <span class="attr">foo</span>: <span class="string">"hellow world"</span> &#125; );</span><br><span class="line">    <span class="built_in">console</span>.log( <span class="string">"index 页面加载"</span> )</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">// 页面进入焦点状态: 说白了就是页面在前台显示了</span></span><br><span class="line">  onShow() &#123;</span><br><span class="line">    <span class="built_in">console</span>.log( <span class="string">"index onShow"</span> )</span><br><span class="line">  &#125;,</span><br><span class="line">  onHide() &#123;</span><br><span class="line">    <span class="built_in">console</span>.log( <span class="string">"index onHide"</span> )</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">// 当页面准备好了, 页面上的数据渲染完成了, 图形界面渲染完了</span></span><br><span class="line">  onReady() &#123;</span><br><span class="line">    <span class="comment">// 只有页面加载完成才可以设置标题</span></span><br><span class="line">    wx.setNavigationBarTitle(&#123;</span><br><span class="line">      title: <span class="string">'foo'</span>,</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="built_in">console</span>.log( <span class="string">"ready"</span> )</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 页面卸载: 当页面被销毁掉</span></span><br><span class="line">  <span class="comment">// 正常跳转, 不会销毁, 会一张叠一张, 俗称页面堆栈</span></span><br><span class="line">  <span class="comment">// A 跳到 B, A不会被销毁, 在 B 上面再通过返回键跳回 A, B就被销毁了</span></span><br><span class="line">  onUnload() &#123;</span><br><span class="line">    <span class="comment">// 可以用于在页面卸载之前, 记录保存一些信息</span></span><br><span class="line">    <span class="built_in">console</span>.log( <span class="string">"页面被销毁了"</span> )</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="数据绑定和-wxs"><a href="#数据绑定和-wxs" class="headerlink" title="数据绑定和 wxs"></a>数据绑定和 wxs</h2><p>微信小程序中是单项数据绑定的, 数据源中定义的数据, 可以直接通过小胡子语法映射到页面中</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Page(&#123;</span><br><span class="line">  <span class="comment">// 数据源</span></span><br><span class="line">  data: &#123;</span><br><span class="line">    <span class="comment">// 这个对象里面的属性, 在整个页面中都可以访问到</span></span><br><span class="line">    message: <span class="string">"hello world"</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- mustance 小胡子语法, 专门用来输出逻辑层暴露的数据  --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">text</span>&gt;</span>&#123;&#123; message &#125;&#125;<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- mustance 可以作用在元素的 内容 和 属性上  --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">input</span> <span class="attr">value</span>=<span class="string">'&#123;&#123; message &#125;&#125;'</span>&gt;</span><span class="tag">&lt;/<span class="name">input</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">&lt;!-- 写在小程序内 wxml 中的值, 属性值在双引号内部, 永远是字符串  --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">checkbox</span> <span class="attr">checked</span>=<span class="string">"checked"</span>&gt;</span><span class="tag">&lt;/<span class="name">checkbox</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">&lt;!-- 想要传一个真正意义上的 false  --&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- mustance 可以用于声明有类型的值  --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">checkbox</span> <span class="attr">checked</span>=<span class="string">"&#123;&#123; false &#125;&#125;"</span>&gt;</span><span class="tag">&lt;/<span class="name">checkbox</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><strong>tips</strong>: 数据源只能暴露数据, 不能暴露函数, 就算在数据源中写了函数, 在页面中也是无法调用的, 这跟我们传统的 vue 中的写法, 就有区别了, 要注意!!!</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">Page(&#123;</span><br><span class="line">  <span class="comment">// 数据源</span></span><br><span class="line">  data: &#123;</span><br><span class="line">    <span class="comment">// 这个对象里面的属性, 在整个页面中都可以访问到</span></span><br><span class="line">    message: <span class="string">"hello world"</span>,</span><br><span class="line">    <span class="comment">// 注意: 数据源只能暴露数据, 不能暴露函数, 这样写在页面中也拿不到这个方法!!!</span></span><br><span class="line">    addPofix( input ) &#123;</span><br><span class="line">      <span class="keyword">return</span> input + <span class="string">"$$"</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p>那么如何在视图层定义一个函数呢 ?<br>wxs 标签类似于咱们 js 中的 script 标签, 可以定义一些行内脚本, 这里的代码必须遵循 commonjs 规范</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- mustance 小胡子语法, 专门用来输出逻辑层暴露的数据  --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">text</span>&gt;</span>&#123;&#123; message &#125;&#125;<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- mustance 可以作用在元素的 内容 和 属性上  --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">input</span> <span class="attr">value</span>=<span class="string">'&#123;&#123; message &#125;&#125;'</span>&gt;</span><span class="tag">&lt;/<span class="name">input</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">&lt;!-- 写在小程序内 wxml 中的值, 属性值在双引号内部, 永远是字符串  --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">checkbox</span> <span class="attr">checked</span>=<span class="string">"checked"</span>&gt;</span><span class="tag">&lt;/<span class="name">checkbox</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">&lt;!-- 想要传一个真正意义上的 false  --&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- mustance 可以用于声明有类型的值  --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">checkbox</span> <span class="attr">checked</span>=<span class="string">"&#123;&#123; false &#125;&#125;"</span>&gt;</span><span class="tag">&lt;/<span class="name">checkbox</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">input</span> <span class="attr">value</span>=<span class="string">'&#123;&#123; foo.addPostfix( message ) &#125;&#125;'</span>&gt;</span><span class="tag">&lt;/<span class="name">input</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- wxs  --&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- wxs 标签类似于咱们 js 中的 script 标签, 可以定义一些行内脚本, 这里的代码必须遵循 commonjs 规范 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">wxs</span> <span class="attr">module</span>=<span class="string">"foo"</span>&gt;</span></span><br><span class="line">  // 这里可以导出一个对象, 这个对象可以在页面中直接使用</span><br><span class="line">  // 通过 commonjs 规范导出成员</span><br><span class="line">  module.exports = &#123;</span><br><span class="line">    addPostfix: function( input ) &#123;</span><br><span class="line">      return input + "$$$wxs"</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">wxs</span>&gt;</span></span><br></pre></td></tr></table></figure>
<hr>
<h2 id="控制属性-条件渲染"><a href="#控制属性-条件渲染" class="headerlink" title="控制属性(条件渲染)"></a>控制属性(条件渲染)</h2><h3 id="wx-if-wx-elif-wx-else"><a href="#wx-if-wx-elif-wx-else" class="headerlink" title="wx:if   wx:elif  wx:else"></a>wx:if   wx:elif  wx:else</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">Page(&#123;</span><br><span class="line">  data: &#123;</span><br><span class="line">    isLoading: <span class="literal">true</span></span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  onReady() &#123;</span><br><span class="line">    setTimeout(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">this</span>.setData(&#123; <span class="attr">isLoading</span>: <span class="literal">false</span> &#125; )</span><br><span class="line">    &#125;, <span class="number">2000</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 注意: 在小程序中, 就算在 wx:if 里面, 也要使用小胡子语法 &#123;&#123;&#125;&#125;  --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">view</span> <span class="attr">wx:if</span>=<span class="string">"&#123;&#123; isLoading &#125;&#125;"</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">text</span>&gt;</span>loading...<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- &lt;view wx:elif=""&gt;&lt;/view&gt; --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">view</span> <span class="attr">wx:else</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">text</span>&gt;</span>load done<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>tips:  上面这种做法虽然可以, 但是这样做不太好, </p>
<p>对于频繁需要切换显示状态的元素, 不应该用 wx:if, 应该用 hidden 性能会更高, 因为 wx:if 会不断创建销毁元素, 而hidden是切换 元素的显示状态</p>
</blockquote>
<h3 id="hidden"><a href="#hidden" class="headerlink" title="hidden"></a>hidden</h3><p>hidden 根据boolean 值, 切换显示隐藏状态, 相对于上面wx:if这种操作方式, 性能效率更高</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span> <span class="attr">hidden</span>=<span class="string">"&#123;&#123; !isLoading &#125;&#125;"</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">text</span>&gt;</span>loading...<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">view</span> <span class="attr">hidden</span>=<span class="string">"&#123;&#123; isLoading &#125;&#125;"</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">text</span>&gt;</span>load done<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="block-包装元素"><a href="#block-包装元素" class="headerlink" title="block 包装元素"></a>block 包装元素</h3><p>block 只是一个包装元素, 不会页面中已经存在的其他元素结构造成影响</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- block 只是一个包装元素, 不会对页面中的元素有影响 --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">block</span> <span class="attr">wx:if</span>=<span class="string">"&#123;&#123; isLoading &#125;&#125;"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">text</span>&gt;</span> hello <span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">text</span>&gt;</span> world <span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">block</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">text</span>&gt;</span>不要隐藏我<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure>
<hr>
<h2 id="控制属性-列表渲染"><a href="#控制属性-列表渲染" class="headerlink" title="控制属性(列表渲染)"></a>控制属性(列表渲染)</h2><h3 id="wx-for"><a href="#wx-for" class="headerlink" title="wx:for"></a>wx:for</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">Page(&#123;</span><br><span class="line">  data: &#123;</span><br><span class="line">    students: [</span><br><span class="line">      &#123; <span class="attr">id</span>: <span class="number">1</span>, <span class="attr">name</span>: <span class="string">"张三"</span>, <span class="attr">age</span>: <span class="number">18</span> &#125;,</span><br><span class="line">      &#123; <span class="attr">id</span>: <span class="number">2</span>, <span class="attr">name</span>: <span class="string">"李四"</span>, <span class="attr">age</span>: <span class="number">20</span> &#125;,</span><br><span class="line">      &#123; <span class="attr">id</span>: <span class="number">3</span>, <span class="attr">name</span>: <span class="string">"王五"</span>, <span class="attr">age</span>: <span class="number">24</span> &#125;,</span><br><span class="line">      &#123; <span class="attr">id</span>: <span class="number">4</span>, <span class="attr">name</span>: <span class="string">"赵六"</span>, <span class="attr">age</span>: <span class="number">15</span> &#125;,</span><br><span class="line">      &#123; <span class="attr">id</span>: <span class="number">5</span>, <span class="attr">name</span>: <span class="string">"田七"</span>, <span class="attr">age</span>: <span class="number">32</span> &#125;,</span><br><span class="line">      &#123; <span class="attr">id</span>: <span class="number">6</span>, <span class="attr">name</span>: <span class="string">"王八"</span>, <span class="attr">age</span>: <span class="number">35</span> &#125;</span><br><span class="line">    ]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p>wx:for 默认遍历后, 通过 item 访问单项, index 访问索引<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">view</span> <span class="attr">wx:for</span>=<span class="string">"&#123;&#123; students &#125;&#125;"</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 默认约定通过 item 访问单项, index 访问索引  --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">text</span>&gt;</span>索引: &#123;&#123; index &#125;&#125;, <span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">text</span>&gt;</span>name: &#123;&#123; item.name &#125;&#125;, <span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">text</span>&gt;</span>age: &#123;&#123; item.age &#125;&#125;<span class="tag">&lt;/<span class="name">text</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>当然也是可以定制的, 通过 wx:for-item 可以自定义 item<br>通过 wx:for-index 可以自定义 index</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">view</span> <span class="attr">wx:for</span>=<span class="string">"&#123;&#123; students &#125;&#125;"</span> <span class="attr">wx:for-item</span>=<span class="string">"v"</span> <span class="attr">wx:for-index</span>=<span class="string">"i"</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 默认约定通过 item 访问单项, index 访问索引  --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 也可以通过 wx:for-item="", wx-for-index="" 定制  --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">text</span>&gt;</span>索引: &#123;&#123; i &#125;&#125;, <span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">text</span>&gt;</span>name: &#123;&#123; v.name &#125;&#125;, <span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">text</span>&gt;</span>age: &#123;&#123; v.age &#125;&#125;<span class="tag">&lt;/<span class="name">text</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure>
<ul>
<li>Now you can provide attr “wx:key” for a “wx:for” to improve performance.</li>
<li>下面一般会弹出一个警告, 加 wx:key 可以解决</li>
</ul>
<hr>
<h3 id="wx-key"><a href="#wx-key" class="headerlink" title="wx:key"></a>wx:key</h3><p>我们在数据源中数组中的每个元素其实跟页面中列表的每一行, 其实是对应上的, 后期一旦动态改变了数组源, 页面中肯定也会变<br>所以应该存在一一对应关系</p>
<p><img src="http://static.zybuluo.com/JepsonCC/0itc0augj3km1zw9ciz2nlix/image_1c4rgt7lv1d1irv41ovoae6lec9.png" alt="wx:key对应关系1"></p>
<p>现在勾选上了李四和王五, 然后我们通过 push 往数组中加东西是没问题的, 但是如果通过 unshift 来在上面加一两项以后</p>
<p><strong>结果令人震惊!!! 选中的元素错位了, 我选的明明是李四和王五, 现在对应关系却变了</strong></p>
<p><img src="http://static.zybuluo.com/JepsonCC/x3oxomukswnrxe2x0aa8srsp/image_1c4rh4m5v1mma1din19g11j4n19nqm.png" alt="wx:key对应关系2"></p>
<p>代码如下: </p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">view</span> <span class="attr">wx:for</span>=<span class="string">"&#123;&#123; students &#125;&#125;"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">checkbox</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">text</span>&gt;</span>&#123;&#123; item.name &#125;&#125;<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">bindtap</span>=<span class="string">'addItemHandle'</span>&gt;</span>添加一条数据<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">Page(&#123;</span><br><span class="line">  data: &#123;</span><br><span class="line">    students: [</span><br><span class="line">      &#123; <span class="attr">id</span>: <span class="number">1</span>, <span class="attr">name</span>: <span class="string">"张三"</span>, <span class="attr">age</span>: <span class="number">18</span> &#125;,</span><br><span class="line">      &#123; <span class="attr">id</span>: <span class="number">2</span>, <span class="attr">name</span>: <span class="string">"李四"</span>, <span class="attr">age</span>: <span class="number">20</span> &#125;,</span><br><span class="line">      &#123; <span class="attr">id</span>: <span class="number">3</span>, <span class="attr">name</span>: <span class="string">"王五"</span>, <span class="attr">age</span>: <span class="number">24</span> &#125;,</span><br><span class="line">      &#123; <span class="attr">id</span>: <span class="number">4</span>, <span class="attr">name</span>: <span class="string">"赵六"</span>, <span class="attr">age</span>: <span class="number">15</span> &#125;,</span><br><span class="line">      &#123; <span class="attr">id</span>: <span class="number">5</span>, <span class="attr">name</span>: <span class="string">"田七"</span>, <span class="attr">age</span>: <span class="number">32</span> &#125;,</span><br><span class="line">      &#123; <span class="attr">id</span>: <span class="number">6</span>, <span class="attr">name</span>: <span class="string">"王八"</span>, <span class="attr">age</span>: <span class="number">35</span> &#125;</span><br><span class="line">    ]</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 页面对象上除了可以定义 生命周期的钩子函数外 还可以定义任何其他函数</span></span><br><span class="line">  <span class="comment">// 这些函数可以作为视图层(界面层) 的事件处理函数</span></span><br><span class="line">  addItemHandle() &#123;</span><br><span class="line">    <span class="keyword">const</span> students = <span class="keyword">this</span>.data.students;</span><br><span class="line">    <span class="keyword">let</span> lastId = students[ students.length - <span class="number">1</span> ].id + <span class="number">1</span></span><br><span class="line">    <span class="comment">// students.push(&#123; id: lastId, name: "鹏鹏" + Math.random(), age: 20 &#125;)</span></span><br><span class="line">    students.unshift(&#123; <span class="attr">id</span>: lastId, <span class="attr">name</span>: <span class="string">"鹏鹏"</span> + <span class="built_in">Math</span>.random(), <span class="attr">age</span>: <span class="number">20</span> &#125;)</span><br><span class="line">    <span class="keyword">this</span>.setData( &#123; students &#125; )</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p>页面中的元素希望实现一一对应的话就需要通过 v:key 来加一个唯一标识</p>
<p><strong>而且, 写法也要注意, wx:key 中写的是遍历单项中的属性名字!!! 大坑!!!</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- 注意: wx:key 中写的是遍历单项中的属性名字  --&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- 如果值本身就不是对象, 没属性成员, 可以通过 *this 这个保留值, </span></span><br><span class="line"><span class="comment">       让自己作为key, 去指定当前被遍历的元素, (这语法真是醉了...)  --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">view</span> <span class="attr">wx:for</span>=<span class="string">"&#123;&#123; students &#125;&#125;"</span> <span class="attr">wx:key</span>=<span class="string">"id"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">checkbox</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">text</span>&gt;</span>&#123;&#123; item.name &#125;&#125;<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">bindtap</span>=<span class="string">'addItemHandle'</span>&gt;</span>添加一条数据<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br></pre></td></tr></table></figure>
<hr>
<h2 id="事件处理"><a href="#事件处理" class="headerlink" title="事件处理"></a>事件处理</h2><p>官网地址: <a href="https://mp.weixin.qq.com/debug/wxadoc/dev/framework/view/wxml/event.html" target="_blank" rel="noopener">https://mp.weixin.qq.com/debug/wxadoc/dev/framework/view/wxml/event.html</a></p>
<p>在组件中绑定一个事件处理函数, 事件前加上 bind 即可</p>
<p>如bindtap，当用户点击该组件的时候会在该页面对应的Page中找到相应的事件处理函数<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span> <span class="attr">bindtap</span>=<span class="string">"viewTapHandle"</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">button</span> <span class="attr">bindtap</span>=<span class="string">"tapHandle"</span>&gt;</span>Click me<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p><strong>默认会事件冒泡</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">Page(&#123;</span><br><span class="line">  <span class="comment">// data</span></span><br><span class="line">  <span class="comment">// on xxx</span></span><br><span class="line">  <span class="comment">// 定义用于界面层的事件处理函数</span></span><br><span class="line">  data: &#123;&#125;,</span><br><span class="line">  tapHandle( e ) &#123;</span><br><span class="line">    <span class="comment">// e 是事件参数</span></span><br><span class="line">    <span class="built_in">console</span>.log( e )</span><br><span class="line">  &#125;,</span><br><span class="line">  viewTapHandle( e ) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log( <span class="string">"viewHandle----"</span> );</span><br><span class="line">    <span class="built_in">console</span>.log( e );</span><br><span class="line">  &#125; </span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></p>
<p>不想冒泡, 正常以前的方法(e.stopPropagation, return false)不会起作用了, 需要使用新的关键字 catch</p>
<p>事件前面加上 catch 即可注册不冒泡的事件</p>
<p>比如: 用 <strong>catchtap 代替 bindtap 绑定事件, 就不会冒泡了</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span> <span class="attr">catchtap</span>=<span class="string">"viewTapHandle"</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">button</span> <span class="attr">catchtap</span>=<span class="string">"tapHandle"</span>&gt;</span>Click me<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="自定义属性"><a href="#自定义属性" class="headerlink" title="自定义属性"></a>自定义属性</h3><ul>
<li>通过 data-…  来传递值, 必须以 data- 开头</li>
<li>可以通过 data- 属性给事件处理函数, 传递额外的参数<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">text</span>&gt;</span>item 1<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">button</span> <span class="attr">data-id</span>=<span class="string">"1"</span> <span class="attr">bindtap</span>=<span class="string">'removeHandle'</span>&gt;</span>remove<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">text</span>&gt;</span>item 2<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">button</span> <span class="attr">data-id</span>=<span class="string">"2"</span> <span class="attr">bindtap</span>=<span class="string">'removeHandle'</span>&gt;</span>remove<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">text</span>&gt;</span>item 3<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">button</span> <span class="attr">data-id</span>=<span class="string">"3"</span> <span class="attr">bindtap</span>=<span class="string">'removeHandle'</span>&gt;</span>remove<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">text</span>&gt;</span>item 4<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">button</span> <span class="attr">data-id</span>=<span class="string">"4"</span> <span class="attr">bindtap</span>=<span class="string">'removeHandle'</span>&gt;</span>remove<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">text</span>&gt;</span>item 5<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">button</span> <span class="attr">data-id</span>=<span class="string">"5"</span> <span class="attr">bindtap</span>=<span class="string">'removeHandle'</span>&gt;</span>remove<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>通过事件对象 e.currentTarget.dataset 拿到所有的自定义属性集合<br>而且如果自定义属性有连字符, 都会被去掉, 变成驼峰命名<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">removeHandle( e ) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log( e.currentTarget.dataset.id )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="单向数据流"><a href="#单向数据流" class="headerlink" title="单向数据流"></a>单向数据流</h2><p>在微信小程序中的数据源和页面的映射是单向的, 数据源中的数据改变后会映射到页面中, 但是页面中的数据变化了, 不会直接改变数据源中的数据, 如果希望实现双向数据绑定, 还是需要通过 js 来 setData 来手动改变数据源的</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">text</span>&gt;</span>&#123;&#123; foo &#125;&#125;<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 文本框发送变化时触发  --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">value</span>=<span class="string">"&#123;&#123; foo &#125;&#125;"</span> <span class="attr">bindinput</span>=<span class="string">"inputChangeHandle"</span>&gt;</span><span class="tag">&lt;/<span class="name">input</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">Page(&#123;</span><br><span class="line">  data: &#123;</span><br><span class="line">    foo: <span class="string">"Jepson"</span></span><br><span class="line">  &#125;,</span><br><span class="line">  inputChangeHandle( e ) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log( e );</span><br><span class="line">    <span class="comment">// 将页面中的数据同步回数据源上</span></span><br><span class="line">    <span class="comment">// this.data.foo = e.detail.value</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1. 改变数据源</span></span><br><span class="line">    <span class="comment">// 2. 通知数据源改变了, 需要重新渲染页面</span></span><br><span class="line">    <span class="keyword">this</span>.setData( &#123; <span class="attr">foo</span>: e.detail.value &#125; )</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="setData-设置多层级数据源的数据"><a href="#setData-设置多层级数据源的数据" class="headerlink" title="setData 设置多层级数据源的数据"></a>setData 设置多层级数据源的数据</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">this</span>.setData(&#123; <span class="string">'category.id'</span>: cat  &#125; )</span><br></pre></td></tr></table></figure>
<h2 id="WXSS"><a href="#WXSS" class="headerlink" title="WXSS"></a>WXSS</h2><p>weui 有一套专门针对小程序做的 ui 地址: <a href="https://weui.io/" target="_blank" rel="noopener">https://weui.io/</a><br>正常和 css 一样写就可以了,<br>单位可以用 px 也可以用 rpx, rpx和rem很像<br>小程序把页面当成了宽度750的尺寸(按照iphone6的尺寸来的), 你用 rpx 作为单位, 就相当于在宽度 750 的尺寸中布局即可, 写成 375rpx 就相当于设成 宽度 50%了, 且可以自动在不同屏幕下适配, 使用起来非常方便<br><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* pages/wxss/wxss.wxss */</span></span><br><span class="line"></span><br><span class="line">// 导入 wxss 文件</span><br><span class="line">@<span class="keyword">import</span> <span class="string">"../../common.wxss"</span></span><br><span class="line"></span><br><span class="line">.box &#123;</span><br><span class="line">  <span class="selector-tag">width</span>: 300<span class="selector-tag">rpx</span>;</span><br><span class="line">  <span class="selector-tag">height</span>: 400<span class="selector-tag">rpx</span>;</span><br><span class="line">  <span class="selector-tag">font-size</span>: 14<span class="selector-tag">px</span>;</span><br><span class="line">  <span class="selector-tag">background-color</span>: <span class="selector-tag">pink</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="request-请求"><a href="#request-请求" class="headerlink" title="request 请求"></a>request 请求</h2><p>小程序中因为是手机端, 不存在浏览器端才有的跨域问题, 所以直接通过官方提供的 <code>wx.request</code> 方法就可以进行请求获取数据</p>
<p>但是, 小程序自身为了安全有个限制, 你所请求数据的网站地址, 必须配置在小程序管理后台 -&gt; 设置 -&gt; 开发设置 -&gt; 服务器域名中, 且 必须是 https 协议, 域名必须要备案.</p>
<ul>
<li>当然小程序在开发工具中测试时, 可以不校验 (但是真实上线还是要备案,要https协议才能使用的, 这只不过时掩耳盗铃罢了)<br><img src="http://static.zybuluo.com/JepsonCC/3x9ssoktx4wydhmid3tzbr1z/image_1c50e4cpg9hr19pvq041ttq1c7mm.png" alt="image_1c50e4cpg9hr19pvq041ttq1c7mm.png-24.5kB"></li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 生命周期函数--监听页面加载</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">onLoad: <span class="function"><span class="keyword">function</span> (<span class="params">options</span>) </span>&#123;</span><br><span class="line">  wx.request(&#123;</span><br><span class="line">    <span class="comment">// 在手机上没有所谓的域, 所以没有跨域的概念</span></span><br><span class="line">    url: <span class="string">'https://locally.uieee.com/slides'</span>,</span><br><span class="line">    data: &#123;&#125;,</span><br><span class="line">    method: <span class="string">'GET'</span>, <span class="comment">// OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT</span></span><br><span class="line">    <span class="comment">// header: &#123;&#125;, // 设置请求的 header</span></span><br><span class="line">    success: <span class="function"><span class="keyword">function</span>(<span class="params">res</span>)</span>&#123;</span><br><span class="line">      <span class="comment">// success</span></span><br><span class="line">      <span class="built_in">console</span>.log( res )</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<h3 id="request-工具函数封装"><a href="#request-工具函数封装" class="headerlink" title="request 工具函数封装"></a>request 工具函数封装</h3><p>当然页面中的请求是有很多个的, 我们考虑封装一下, 将来实现可以实现复用<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = <span class="function">(<span class="params">url, data</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 通过一个异步事件初始化一个 Promise 对象</span></span><br><span class="line">    wx.request(&#123;</span><br><span class="line">      url: <span class="string">`https://locally.uieee.com/<span class="subst">$&#123;url&#125;</span>`</span>,</span><br><span class="line">      data: data,</span><br><span class="line">      method: <span class="string">'GET'</span>, </span><br><span class="line">      <span class="comment">// 许诺成功了</span></span><br><span class="line">      success: resolve,</span><br><span class="line">      <span class="comment">// 许诺失败了</span></span><br><span class="line">      fail: reject</span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>使用封装的 fetch 方法进行请求<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 轮播图数据渲染</span></span><br><span class="line">fetch( <span class="string">"slides"</span> ).then(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">this</span>.setData(&#123; <span class="string">"imgUrls"</span>: res.data &#125;);</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// grid 列表渲染</span></span><br><span class="line">fetch(<span class="string">"categories"</span>).then(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">this</span>.setData(&#123; <span class="string">"gridsList"</span>: res.data &#125;);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></p>
<h2 id="页面间跳转-通过-key-value传参"><a href="#页面间跳转-通过-key-value传参" class="headerlink" title="页面间跳转(通过 ?key=value传参)"></a>页面间跳转(通过 ?key=value传参)</h2><p>官方api: <a href="https://mp.weixin.qq.com/debug/wxadoc/dev/component/navigator.html" target="_blank" rel="noopener">https://mp.weixin.qq.com/debug/wxadoc/dev/component/navigator.html</a><br>url 表示应用内跳转, 如果跳转的页面是底部tab选项卡的话需要设置<code>open-type</code>为switchTab<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span> <span class="attr">class</span>=<span class="string">"grids"</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">navigator</span> <span class="attr">url</span>=<span class="string">"/pages/message/message"</span> <span class="attr">open-type</span>=<span class="string">"switchTab"</span> <span class="attr">class</span>=<span class="string">"item"</span> <span class="attr">wx:for</span>=<span class="string">"&#123;&#123;gridsList&#125;&#125;"</span> <span class="attr">wx:key</span>=<span class="string">"unique"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">image</span> <span class="attr">src</span>=<span class="string">"&#123;&#123;item.icon&#125;&#125;"</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">text</span>&gt;</span>&#123;&#123;item.name&#125;&#125;<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">navigator</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>在page间跳转, 可以通过 queryString 传参<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span> <span class="attr">class</span>=<span class="string">"grids"</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">navigator</span> <span class="attr">url</span>=<span class="string">"/pages/listpage/listpage?cat=&#123;&#123;item.id&#125;&#125;"</span> <span class="attr">class</span>=<span class="string">"item"</span> <span class="attr">wx:for</span>=<span class="string">"&#123;&#123;gridsList&#125;&#125;"</span> <span class="attr">wx:key</span>=<span class="string">"unique"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">image</span> <span class="attr">src</span>=<span class="string">"&#123;&#123;item.icon&#125;&#125;"</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">text</span>&gt;</span>&#123;&#123;item.name&#125;&#125;<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">navigator</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>在跳转到的页面中, 通过 options 拿到传递过来的参数<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 生命周期函数--监听页面加载</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">onLoad: <span class="function"><span class="keyword">function</span> (<span class="params">options</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log( options )</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure></p>
<h3 id="手动页面跳转"><a href="#手动页面跳转" class="headerlink" title="手动页面跳转"></a>手动页面跳转</h3><p>页面路由: <a href="https://mp.weixin.qq.com/debug/wxadoc/dev/framework/app-service/route.html" target="_blank" rel="noopener">https://mp.weixin.qq.com/debug/wxadoc/dev/framework/app-service/route.html</a><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 使用 navigateTo 进行跳转会产生父子页面关系(在下一个页面可以返回上一个页面), 小程序规定, 最多这个层级关系只能 5 层</span></span><br><span class="line">wx.navigateTo(&#123; <span class="attr">url</span>: <span class="string">"../message/message"</span> &#125;)</span><br><span class="line"><span class="comment">// 注意: navigateTo 只能打开 非 tab 页面</span></span><br><span class="line"></span><br><span class="line">wx.switchTab(&#123; <span class="attr">url</span>: <span class="string">"../message/message"</span>&#125; )</span><br><span class="line"><span class="comment">// 注意: switch 只能打开 tab 页面</span></span><br><span class="line">   </span><br><span class="line">   </span><br><span class="line"><span class="comment">// 如果不希望让跳转的页面成为子页面, 要用重定向方法</span></span><br><span class="line"><span class="comment">// redirectTo 会触发页面的销毁事件, 也就是上一个页面被销毁了,</span></span><br><span class="line"><span class="comment">// 而 navigateTo 只是将页面先隐藏了而已</span></span><br><span class="line">wx.redirectTo(&#123; <span class="attr">url</span>: <span class="string">"../main/main"</span>&#125; )</span><br></pre></td></tr></table></figure></p>
<h2 id="关闭下拉刷新"><a href="#关闭下拉刷新" class="headerlink" title="关闭下拉刷新"></a>关闭下拉刷新</h2><p><a href="https://mp.weixin.qq.com/debug/wxadoc/dev/api/pulldown.html#wxstoppulldownrefresh" target="_blank" rel="noopener">https://mp.weixin.qq.com/debug/wxadoc/dev/api/pulldown.html#wxstoppulldownrefresh</a><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wx.stopPullDownRefresh()</span><br></pre></td></tr></table></figure></p>
<h2 id="快捷格式化"><a href="#快捷格式化" class="headerlink" title="快捷格式化"></a>快捷格式化</h2><p><code>alt + shift + F</code></p>
<h2 id="template-模板的使用"><a href="#template-模板的使用" class="headerlink" title="template 模板的使用"></a>template 模板的使用</h2><p>template 模板, 可以复用 wxml  和 wxss </p>
<ul>
<li><p>1 模板提取, 新建模板文件</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span> <span class="attr">name</span>=<span class="string">"list-item"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">view</span> <span class="attr">class</span>=<span class="string">'item'</span> <span class="attr">bindtap</span>=<span class="string">"go"</span> <span class="attr">data-id</span>=<span class="string">"&#123;&#123;item.id&#125;&#125;"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">view</span> <span class="attr">class</span>=<span class="string">'pic'</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">image</span> <span class="attr">src</span>=<span class="string">"&#123;&#123; item.images[0] &#125;&#125;"</span>&gt;</span><span class="tag">&lt;/<span class="name">image</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">view</span> <span class="attr">class</span>=<span class="string">'info'</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">text</span> <span class="attr">class</span>=<span class="string">'title'</span>&gt;</span>&#123;&#123; item.name &#125;&#125;<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">text</span>&gt;</span>电话: &#123;&#123; item.phone &#125;&#125;<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">text</span>&gt;</span>地址: &#123;&#123; item.address &#125;&#125;<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">text</span>&gt;</span>营业时间: &#123;&#123; item.businessHours &#125;&#125; <span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">view</span> <span class="attr">class</span>=<span class="string">'tips'</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">text</span> <span class="attr">class</span>=<span class="string">'score'</span>&gt;</span>&#123;&#123; item.score &#125;&#125;<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>2 在页面中, 引入模板文件</p>
</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">import</span> <span class="attr">src</span>=<span class="string">"./list-item/list-item-template.wxml"</span>/&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 使用 模板  --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">view</span> <span class="attr">class</span>=<span class="string">'goodslist'</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">block</span> <span class="attr">wx:for</span>=<span class="string">"&#123;&#123;shops&#125;&#125;"</span> <span class="attr">wx:key</span>=<span class="string">"id"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">template</span> <span class="attr">is</span>=<span class="string">"list-item"</span> <span class="attr">data</span>=<span class="string">"&#123;&#123; item &#125;&#125;"</span> /&gt;</span></span><br><span class="line">    </span><br><span class="line">  <span class="tag">&lt;/<span class="name">block</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure>
<ul>
<li>3 提取 wxss</li>
</ul>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 商品列表  */</span></span><br><span class="line"><span class="selector-class">.goodslist</span> &#123;</span><br><span class="line">  <span class="attribute">display</span>: flex;</span><br><span class="line">  <span class="attribute">flex-direction</span>: column;</span><br><span class="line">  <span class="attribute">background-color</span>: white;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.goodslist</span> <span class="selector-class">.item</span> &#123;</span><br><span class="line">  <span class="attribute">display</span>: flex;</span><br><span class="line">  <span class="attribute">padding</span>: <span class="number">20</span>rpx;</span><br><span class="line">  <span class="attribute">border-bottom</span>: <span class="number">1</span>rpx solid <span class="number">#eee</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.goodslist</span> <span class="selector-class">.item</span><span class="selector-pseudo">:last-child</span> &#123;</span><br><span class="line">  <span class="attribute">border-bottom</span>: none;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.goodslist</span> <span class="selector-class">.item</span> <span class="selector-class">.pic</span> &#123;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">150</span>rpx;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">150</span>rpx;</span><br><span class="line">  <span class="attribute">padding</span>: <span class="number">0</span> <span class="number">20</span>rpx;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.goodslist</span> <span class="selector-class">.item</span> <span class="selector-class">.pic</span> <span class="selector-tag">image</span> &#123;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">100%</span>;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">100%</span>;</span><br><span class="line">&#125;</span><br><span class="line">....</span><br></pre></td></tr></table></figure>
<ul>
<li>4 在 wxss 中引用即可</li>
</ul>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@<span class="keyword">import</span> <span class="string">"./list-item/list-item-template.wxss"</span>;</span><br></pre></td></tr></table></figure>
<h3 id="模板传参支持参数平铺"><a href="#模板传参支持参数平铺" class="headerlink" title="模板传参支持参数平铺"></a>模板传参支持参数平铺</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">import</span> <span class="attr">src</span>=<span class="string">"./list-item/list-item-template.wxml"</span>/&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 使用 模板  --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">view</span> <span class="attr">class</span>=<span class="string">'goodslist'</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">block</span> <span class="attr">wx:for</span>=<span class="string">"&#123;&#123;shops&#125;&#125;"</span> <span class="attr">wx:key</span>=<span class="string">"id"</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 使用 参数平铺将数据对象属性 平铺  --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">template</span> <span class="attr">is</span>=<span class="string">"list-item"</span> <span class="attr">data</span>=<span class="string">"&#123;&#123; ...item &#125;&#125;"</span> /&gt;</span></span><br><span class="line">    </span><br><span class="line">  <span class="tag">&lt;/<span class="name">block</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>模板中直接使用属性名即可<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span> <span class="attr">name</span>=<span class="string">"list-item"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">view</span> <span class="attr">class</span>=<span class="string">'item'</span> <span class="attr">bindtap</span>=<span class="string">"go"</span> <span class="attr">data-id</span>=<span class="string">"&#123;&#123;id&#125;&#125;"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">view</span> <span class="attr">class</span>=<span class="string">'pic'</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">image</span> <span class="attr">src</span>=<span class="string">"&#123;&#123; images[0] &#125;&#125;"</span>&gt;</span><span class="tag">&lt;/<span class="name">image</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">view</span> <span class="attr">class</span>=<span class="string">'info'</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">text</span> <span class="attr">class</span>=<span class="string">'title'</span>&gt;</span>&#123;&#123; name &#125;&#125;<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">text</span>&gt;</span>电话: &#123;&#123; phone &#125;&#125;<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">text</span>&gt;</span>地址: &#123;&#123; address &#125;&#125;<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">text</span>&gt;</span>营业时间: &#123;&#123; businessHours &#125;&#125; <span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">view</span> <span class="attr">class</span>=<span class="string">'tips'</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">text</span> <span class="attr">class</span>=<span class="string">'score'</span>&gt;</span>&#123;&#123; score &#125;&#125;<span class="tag">&lt;/<span class="name">text</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<h2 id="数据缓存-storage"><a href="#数据缓存-storage" class="headerlink" title="数据缓存 storage"></a>数据缓存 storage</h2><p><a href="https://mp.weixin.qq.com/debug/wxadoc/dev/api/data.html#wxsetstorageobject" target="_blank" rel="noopener">https://mp.weixin.qq.com/debug/wxadoc/dev/api/data.html#wxsetstorageobject</a><br>如果用户不主动去清除这个缓存, 这个缓存是一直存在的</p>
<p>tips: 本地数据存储的大小限制为 10MB !</p>
<h3 id="设置缓存"><a href="#设置缓存" class="headerlink" title="设置缓存"></a>设置缓存</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 这种是异步的, 还有一个同步的 wx.setStorageSync('key', 'value')</span></span><br><span class="line">wx.setStorage(&#123;</span><br><span class="line">  key: <span class="string">"search_key"</span>,</span><br><span class="line">  data: <span class="string">"风暴英雄"</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p>支持存储 object 对象<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">wx.setStorage(&#123;</span><br><span class="line">  key: <span class="string">"search_key"</span>,</span><br><span class="line">  data: &#123;</span><br><span class="line">    name: <span class="string">"jepson"</span>,</span><br><span class="line">    age: <span class="number">18</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></p>
<h3 id="获取缓存"><a href="#获取缓存" class="headerlink" title="获取缓存"></a>获取缓存</h3><p>获取到的缓存数据就在 success 回调 res.data 中<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">wx.getStorage(&#123;</span><br><span class="line">  key: <span class="string">'search_key'</span>,</span><br><span class="line">  success: <span class="function"><span class="keyword">function</span>(<span class="params">res</span>) </span>&#123;</span><br><span class="line">      <span class="built_in">console</span>.log(res.data)</span><br><span class="line">  &#125; </span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></p>
<h3 id="删除缓存"><a href="#删除缓存" class="headerlink" title="删除缓存"></a>删除缓存</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">wx.removeStorage(&#123;</span><br><span class="line">  key: <span class="string">"search_key"</span>,</span><br><span class="line">  success( res ) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log( res )</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="清空缓存"><a href="#清空缓存" class="headerlink" title="清空缓存"></a>清空缓存</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wx.clearStorage()</span><br></pre></td></tr></table></figure>
<p>注意: 这里的几种不加Sync的方法, 都是异步的!</p>
<h2 id="交互反馈-wx-toast-等弹出框"><a href="#交互反馈-wx-toast-等弹出框" class="headerlink" title="交互反馈 wx.toast 等弹出框"></a>交互反馈 wx.toast 等弹出框</h2><h3 id="wx-toast-提示框"><a href="#wx-toast-提示框" class="headerlink" title="wx.toast 提示框"></a>wx.toast 提示框</h3><p>官方 api: <a href="https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-react.html#wxshowtoastobject" target="_blank" rel="noopener">https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-react.html#wxshowtoastobject</a><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">wx.showToast(&#123;</span><br><span class="line">    title: <span class="string">"数据加载成功"</span>,</span><br><span class="line">    icon: <span class="string">"success"</span>,</span><br><span class="line">    duration: <span class="number">1000</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></p>
<h3 id="wx-showLoading-显示提示框"><a href="#wx-showLoading-显示提示框" class="headerlink" title="wx.showLoading 显示提示框"></a>wx.showLoading 显示提示框</h3><p>显示提示框, 关闭必须手动调用 hideLoading<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">wx.showLoading()</span><br><span class="line"></span><br><span class="line"><span class="comment">// 模拟加载延时, 1秒后可以关闭 loading 提示框</span></span><br><span class="line">setTimeout(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">  wx.hideLoading()</span><br><span class="line">&#125;, <span class="number">1000</span>)</span><br></pre></td></tr></table></figure></p>
<p>添加透明蒙层, 可以防止触摸穿透, 默认是 false, 就是默认手是可以点按页面中的元素的, 但是一旦设置了 <code>mask: true</code>, 用户就点不到下面的元素了<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">wx.showLoading(&#123;</span><br><span class="line">  mask: <span class="literal">true</span>  <span class="comment">// 添加透明蒙层, 可以防止触摸穿透, 默认 false</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></p>
<h3 id="wx-showModal-展示模态框"><a href="#wx-showModal-展示模态框" class="headerlink" title="wx.showModal 展示模态框"></a>wx.showModal 展示模态框</h3><p><a href="https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-react.html#wxshowmodalobject" target="_blank" rel="noopener">https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-react.html#wxshowmodalobject</a><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">wx.showModal(&#123;</span><br><span class="line">  title: <span class="string">"标题"</span>, </span><br><span class="line">  content: <span class="string">"你确定要执行此操作么?"</span>,</span><br><span class="line">  success: <span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 通过 res.confirm 来判断到底选的是什么</span></span><br><span class="line">    <span class="keyword">if</span> ( res.confirm ) &#123;</span><br><span class="line">      <span class="built_in">console</span>.log( <span class="string">'用户点击确定'</span> );</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="built_in">console</span>.log( <span class="string">'用户点击退出'</span> )</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></p>
<h3 id="wx-showActionSheet-显示操作菜单"><a href="#wx-showActionSheet-显示操作菜单" class="headerlink" title="wx.showActionSheet 显示操作菜单"></a>wx.showActionSheet 显示操作菜单</h3><p><a href="https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-react.html#wxshowactionsheetobject" target="_blank" rel="noopener">https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-react.html#wxshowactionsheetobject</a><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">wx.showActionSheet(&#123;</span><br><span class="line">  itemList: [<span class="string">'分享到朋友圈'</span>, <span class="string">'分享给朋友'</span>],</span><br><span class="line">  success: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// tapIndex 用户点击数组索引号</span></span><br><span class="line">    <span class="built_in">console</span>.log(res.tapIndex)</span><br><span class="line">  &#125;,</span><br><span class="line">  fail: <span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 用于如果点击取消会进入 fail 事件处理函数</span></span><br><span class="line">    <span class="built_in">console</span>.log(res.errMsg)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></p>
<h2 id="图片预览轮播"><a href="#图片预览轮播" class="headerlink" title="图片预览轮播"></a>图片预览轮播</h2><p><a href="https://mp.weixin.qq.com/debug/wxadoc/dev/api/media-picture.html#wxpreviewimageobject" target="_blank" rel="noopener">https://mp.weixin.qq.com/debug/wxadoc/dev/api/media-picture.html#wxpreviewimageobject</a><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 该方法用于图片预览</span></span><br><span class="line">preview( e ) &#123;</span><br><span class="line">    wx.previewImage(&#123;</span><br><span class="line">      current: e.currentTarget.dataset.src, <span class="comment">// 当前显示图片的http链接</span></span><br><span class="line">      urls: <span class="keyword">this</span>.data.shop.images <span class="comment">// 需要预览的图片http链接列表</span></span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="直接跳到页面底部"><a href="#直接跳到页面底部" class="headerlink" title="直接跳到页面底部"></a>直接跳到页面底部</h2><p>前置 api : pageScrollTo<br>将页面滚动到目标位置: <a href="https://mp.weixin.qq.com/debug/wxadoc/dev/api/scroll.html" target="_blank" rel="noopener">https://mp.weixin.qq.com/debug/wxadoc/dev/api/scroll.html</a><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">wx.pageScrollTo(&#123;</span><br><span class="line">  scrollTop: 0,</span><br><span class="line">  duration: 300</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></p>
<p>实现步骤:</p>
<ul>
<li>先用一个 view 占位</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">view</span> <span class="attr">id</span>=<span class="string">"bottom"</span>&gt;</span><span class="tag">&lt;/<span class="name">view</span>&gt;</span></span><br></pre></td></tr></table></figure>
<ul>
<li>onReady后 进行跳转, 到最下面<br>WXML节点信息API: <a href="https://mp.weixin.qq.com/debug/wxadoc/dev/api/wxml-nodes-info.html" target="_blank" rel="noopener">https://mp.weixin.qq.com/debug/wxadoc/dev/api/wxml-nodes-info.html</a><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">onReady () &#123;</span><br><span class="line">    <span class="comment">// 创建查询</span></span><br><span class="line">    <span class="keyword">const</span> query = wx.createSelectorQuery()</span><br><span class="line">    <span class="comment">// </span></span><br><span class="line">    query.select(<span class="string">'#bottom'</span>).boundingClientRect()</span><br><span class="line">    query.exec(<span class="function"><span class="params">res</span> =&gt;</span> wx.pageScrollTo(&#123; <span class="attr">scrollTop</span>: res[<span class="number">0</span>].top + <span class="number">200</span> &#125;))</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="音乐播放控制"><a href="#音乐播放控制" class="headerlink" title="音乐播放控制"></a>音乐播放控制</h2><h3 id="基本背景音乐控制"><a href="#基本背景音乐控制" class="headerlink" title="基本背景音乐控制"></a>基本背景音乐控制</h3><p>官方api: <a href="https://mp.weixin.qq.com/debug/wxadoc/dev/api/media-background-audio.html#wxplaybackgroundaudioobject" target="_blank" rel="noopener">https://mp.weixin.qq.com/debug/wxadoc/dev/api/media-background-audio.html#wxplaybackgroundaudioobject</a></p>
<h4 id="播放背景音乐-playBackgroundAudio"><a href="#播放背景音乐-playBackgroundAudio" class="headerlink" title="播放背景音乐 playBackgroundAudio"></a>播放背景音乐 playBackgroundAudio</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">wx.playBackgroundAudio(&#123;</span><br><span class="line">  title: <span class="string">"夜夜夜夜-齐秦"</span>,</span><br><span class="line">  coverImgUrl: <span class="string">"http://y.gtimg.cn/music/photo_new/T002R150x150M000001TEc6V0kjpVC.jpg?max_age=2592000"</span>,</span><br><span class="line">  dataUrl: <span class="string">"http://ws.stream.qqmusic.qq.com/C100003507bR0gDKBm.m4a?fromtag=38"</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h4 id="关闭背景音乐-pauseBackgroundAudio"><a href="#关闭背景音乐-pauseBackgroundAudio" class="headerlink" title="关闭背景音乐 pauseBackgroundAudio"></a>关闭背景音乐 pauseBackgroundAudio</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wx.pauseBackgroundAudio()</span><br></pre></td></tr></table></figure>
<h4 id="事件监听"><a href="#事件监听" class="headerlink" title="事件监听"></a>事件监听</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">wx.onBackgroundAudioPlay(CALLBACK)</span><br><span class="line">监听音乐播放。</span><br><span class="line"></span><br><span class="line">wx.onBackgroundAudioPause(CALLBACK)</span><br><span class="line">监听音乐暂停。</span><br><span class="line"></span><br><span class="line">wx.onBackgroundAudioStop(CALLBACK)</span><br><span class="line">监听音乐停止。</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="http://static.zybuluo.com/JepsonCC/qi8vjk62dk51zqt0a8an8xir/image_1c4uso3kemnn18jg11951ho31g2o9.png" alt="Jepson 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="http://static.zybuluo.com/JepsonCC/6l63d7x84xii3bwgkpcqk33n/image_1c4usp36f121s29j2r511fd18r5m.png" alt="Jepson 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/微信小程序/" rel="tag"># 微信小程序</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/01/28/git版本控制工具/" rel="prev" title="git版本控制工具">
                git版本控制工具 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="http://static.zybuluo.com/JepsonCC/focrrncqb7uriops1uguvcfd/image_1c4uu558p1vhh71a1mvqbsp11i2m.png"
                alt="Jepson" />
            
              <p class="site-author-name" itemprop="name">Jepson</p>
              <p class="site-description motion-element" itemprop="description">爱学习, 爱前端</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/JepsonGithub" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#微信小程序"><span class="nav-number">1.</span> <span class="nav-text">微信小程序</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#从业务层面看小程序"><span class="nav-number">1.1.</span> <span class="nav-text">从业务层面看小程序</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#开发包必须小于-1-M"><span class="nav-number">1.1.1.</span> <span class="nav-text">开发包必须小于 1 M</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#从技术层面看小程序"><span class="nav-number">1.2.</span> <span class="nav-text">从技术层面看小程序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#微信小程序初体验"><span class="nav-number">1.3.</span> <span class="nav-text">微信小程序初体验</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#基本配置"><span class="nav-number">1.3.1.</span> <span class="nav-text">基本配置</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#全局配置-app-json"><span class="nav-number">1.3.1.1.</span> <span class="nav-text">全局配置 (app.json)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#页面配置-页面-json"><span class="nav-number">1.3.1.2.</span> <span class="nav-text">页面配置 (页面.json)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#标签栏配置-app-json"><span class="nav-number">1.3.1.3.</span> <span class="nav-text">标签栏配置 (app.json)</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#逻辑层js的小-tips"><span class="nav-number">1.4.</span> <span class="nav-text">逻辑层js的小 tips</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#应用的生命周期"><span class="nav-number">1.5.</span> <span class="nav-text">应用的生命周期</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#页面的生命周期"><span class="nav-number">1.6.</span> <span class="nav-text">页面的生命周期</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据绑定和-wxs"><span class="nav-number">1.7.</span> <span class="nav-text">数据绑定和 wxs</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#控制属性-条件渲染"><span class="nav-number">1.8.</span> <span class="nav-text">控制属性(条件渲染)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#wx-if-wx-elif-wx-else"><span class="nav-number">1.8.1.</span> <span class="nav-text">wx:if   wx:elif  wx:else</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#hidden"><span class="nav-number">1.8.2.</span> <span class="nav-text">hidden</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#block-包装元素"><span class="nav-number">1.8.3.</span> <span class="nav-text">block 包装元素</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#控制属性-列表渲染"><span class="nav-number">1.9.</span> <span class="nav-text">控制属性(列表渲染)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#wx-for"><span class="nav-number">1.9.1.</span> <span class="nav-text">wx:for</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#wx-key"><span class="nav-number">1.9.2.</span> <span class="nav-text">wx:key</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#事件处理"><span class="nav-number">1.10.</span> <span class="nav-text">事件处理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#自定义属性"><span class="nav-number">1.10.1.</span> <span class="nav-text">自定义属性</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#单向数据流"><span class="nav-number">1.11.</span> <span class="nav-text">单向数据流</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#setData-设置多层级数据源的数据"><span class="nav-number">1.11.1.</span> <span class="nav-text">setData 设置多层级数据源的数据</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#WXSS"><span class="nav-number">1.12.</span> <span class="nav-text">WXSS</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#request-请求"><span class="nav-number">1.13.</span> <span class="nav-text">request 请求</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#request-工具函数封装"><span class="nav-number">1.13.1.</span> <span class="nav-text">request 工具函数封装</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#页面间跳转-通过-key-value传参"><span class="nav-number">1.14.</span> <span class="nav-text">页面间跳转(通过 ?key=value传参)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#手动页面跳转"><span class="nav-number">1.14.1.</span> <span class="nav-text">手动页面跳转</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#关闭下拉刷新"><span class="nav-number">1.15.</span> <span class="nav-text">关闭下拉刷新</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#快捷格式化"><span class="nav-number">1.16.</span> <span class="nav-text">快捷格式化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#template-模板的使用"><span class="nav-number">1.17.</span> <span class="nav-text">template 模板的使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#模板传参支持参数平铺"><span class="nav-number">1.17.1.</span> <span class="nav-text">模板传参支持参数平铺</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据缓存-storage"><span class="nav-number">1.18.</span> <span class="nav-text">数据缓存 storage</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#设置缓存"><span class="nav-number">1.18.1.</span> <span class="nav-text">设置缓存</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取缓存"><span class="nav-number">1.18.2.</span> <span class="nav-text">获取缓存</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除缓存"><span class="nav-number">1.18.3.</span> <span class="nav-text">删除缓存</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#清空缓存"><span class="nav-number">1.18.4.</span> <span class="nav-text">清空缓存</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#交互反馈-wx-toast-等弹出框"><span class="nav-number">1.19.</span> <span class="nav-text">交互反馈 wx.toast 等弹出框</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#wx-toast-提示框"><span class="nav-number">1.19.1.</span> <span class="nav-text">wx.toast 提示框</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#wx-showLoading-显示提示框"><span class="nav-number">1.19.2.</span> <span class="nav-text">wx.showLoading 显示提示框</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#wx-showModal-展示模态框"><span class="nav-number">1.19.3.</span> <span class="nav-text">wx.showModal 展示模态框</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#wx-showActionSheet-显示操作菜单"><span class="nav-number">1.19.4.</span> <span class="nav-text">wx.showActionSheet 显示操作菜单</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#图片预览轮播"><span class="nav-number">1.20.</span> <span class="nav-text">图片预览轮播</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#直接跳到页面底部"><span class="nav-number">1.21.</span> <span class="nav-text">直接跳到页面底部</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#音乐播放控制"><span class="nav-number">1.22.</span> <span class="nav-text">音乐播放控制</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#基本背景音乐控制"><span class="nav-number">1.22.1.</span> <span class="nav-text">基本背景音乐控制</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#播放背景音乐-playBackgroundAudio"><span class="nav-number">1.22.1.1.</span> <span class="nav-text">播放背景音乐 playBackgroundAudio</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#关闭背景音乐-pauseBackgroundAudio"><span class="nav-number">1.22.1.2.</span> <span class="nav-text">关闭背景音乐 pauseBackgroundAudio</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#事件监听"><span class="nav-number">1.22.1.3.</span> <span class="nav-text">事件监听</span></a></li></ol></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Jepson</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




<div><span><a href="http://www.miitbeian.gov.cn">苏ICP备18002051号-1</a></span></div>

        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
